package cn.spike.pet.controller;

import cn.spike.basic.exception.BusinessException;
import cn.spike.pet.dto.MsgDto;
import cn.spike.pet.service.ISearchMasterMsgService;
import cn.spike.pet.domain.SearchMasterMsg;
import cn.spike.pet.query.SearchMasterMsgQuery;
import cn.spike.basic.util.AjaxResult;
import cn.spike.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult save(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            searchMasterMsgService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg loadOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> loadAll() {
        return searchMasterMsgService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<SearchMasterMsg> pageQuery(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.pageQuery(query);
    }

    /**
     * 接口：发布寻主消息
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            return searchMasterMsgService.publish(searchMasterMsg,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败【500，400，404】");
        }
    }

    /**
     * 接口：用户查询自己发布的所有寻主消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }

    /**
     * 接口：查询待处理的寻主消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query,request);
    }

    /**
     * 接口：查询已完成的寻主消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.finish(query,request);
    }

    /**
     * 接口：查询寻主池的寻主消息
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.toSearchMasterPool(query);
    }


    /**
     * 接口：拒单
     *
     * @param id 寻主消息Id
     * @return PageList 分页对象
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable Long id) {
        try {
            searchMasterMsgService.reject(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败,请检查网络后重试");
        }
    }

    /**
     * 接口：接单
     *
     * @param msgDto
     * @return PageList 分页对象
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto) {
        try {
            searchMasterMsgService.accept(msgDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败,请检查网络后重试");
        }
    }

}
